package com.risenb.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.risenb.bean.Menu;
import com.risenb.bean.ResultMsg;
import com.risenb.service.MenuService;

@Controller
@RequestMapping(value = "/menu")
public class MenuController extends CustomerBaseControllor {
	
	@Autowired
	MenuService menuService;

	/**
	 * 查询所有
	 * 
	 * @return
	 */
	@RequestMapping("/fa")
	public ModelAndView findMenus() {
		List<Menu> ms = menuService.queryMenus();
		return new ModelAndView("/manage/menus").addObject("menus", ms);
	}

	@RequestMapping("/fp")
	public ModelAndView findMenusByParentId(Integer parentid) {
		System.out.println(parentid);
		List<Menu> ms = menuService.queryMenus();
		List<Menu> ms2 = menuService.queryMenusByParentid(parentid);
		return new ModelAndView("/manage/menus").addObject("menus", ms).addObject("menubodys", ms2).addObject("parentid", parentid);
	}

	@RequestMapping("/fi")
	public ModelAndView findMenuById(Integer id, Integer parentid) {
		List<Menu> ms = menuService.queryMenus();
		Menu menu = new Menu();
		if (id != null) {
			menu = menuService.queryMenuById(id);
		} else {
			menu.setParentid(parentid);
		}
		return new ModelAndView("/manage/menus").addObject("menus", ms).addObject("menu", menu).addObject("parentid", parentid);
	}

	@RequestMapping("/em")
	public void editMenu(@RequestBody @ModelAttribute Menu menu, HttpServletRequest request, HttpServletResponse response) throws IOException {
		int row = 0;
		menu.setCreatetime(new Date());
		if (menu.getId() == null) {
			row = menuService.createMenu(menu);
		} else {
			row = menuService.modifyMenuById(menu);
		}
		ResultMsg resultMsg = null;
		if (row > 0) {
			resultMsg = new ResultMsg("200", "操作完成", "/menu/fp.im?parentid=" + menu.getParentid(), "", "", "", "");
		} else {
			resultMsg = new ResultMsg("300", "操作失败", "", "", "", "", "");
		}
		returnJson(resultMsg);
	}

	@RequestMapping("/dm")
	public void deleteMenu(String ids, HttpServletRequest request, HttpServletResponse response) throws IOException {
		int row = menuService.removeMenuByIds(ids);
		ResultMsg resultMsg = null;
		if (row > 0) {
			resultMsg = new ResultMsg("200", "操作完成，本次删除" + row + "条记录", "", "menu", "", "", "");
		} else {
			resultMsg = new ResultMsg("300", "操作失败", "", "menu", "", "", "");
		}
		returnJson(resultMsg);
	}
}
